package com.construction.engineering.accounting.service.config.impl;

import com.construction.engineering.accounting.entity.cea.LogTrace;
import com.construction.engineering.accounting.mapper.LogTraceMapper;
import com.construction.engineering.accounting.service.config.LogTraceService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liangyushijiu
 */
@Service
public class LogTraceServiceImpl implements LogTraceService {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    private final int batchSize = 5000;

    @Resource
    private LogTraceMapper configInfoMapper;


    @Override
    public List<LogTrace> queryLogTrace() {
        return configInfoMapper.queryLogTrace(null);
    }

    @Override
    public String insertLogTrace(String filePath) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);


        List<String> sqlBatch = new ArrayList<>();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            StringBuilder sb = new StringBuilder();
            String line;
            int count = 0;

            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.startsWith("--") || line.isEmpty()) {
                    continue;
                }

                sb.append(line);
                if (line.endsWith(";")) {
                    String sql = sb.toString().replace(";", "").trim();
                    sqlBatch.add(sql);
                    sb = new StringBuilder();
                    count++;

                    // 达到批量大小后执行
                    if (count % batchSize == 0) {
                        executeBatch(jdbcTemplate, sqlBatch);
                        sqlBatch.clear();
                    }
                }
            }

            // 执行剩余的SQL语句
            if (!sqlBatch.isEmpty()) {
                executeBatch(jdbcTemplate, sqlBatch);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return "success";

        }
        return "success";
    }

    private static void executeBatch(JdbcTemplate jdbcTemplate, List<String> sqlBatch) {
        System.out.println("开始执行插入=====》" + sqlBatch.size());
        jdbcTemplate.batchUpdate(sqlBatch.toArray(new String[0]));
        System.out.println("执行完成=====》" + sqlBatch.size());
    }

}
